const tbody = document.querySelector("tbody");
let spanValue,foucsvalue;;
// 渲染
function getScoreList() {
  //发送请求
  axios.get('/score/list').then(res => {
    const obj = res.data.data;
    let trHTML;
    for (const key in obj) {
      const tempHTML = obj[key].score.reduce((h,v,i) => {
        return h += `
                <td class="score">
                <span class="LX">${v}</span>
                <input type="text" value="${v}" select data-batch="${i+1}" data-id="${key}">
                </td>
                `
      }, "")
      trHTML += `
           <tr>
             <th scope="row">${key}</th>
             <td>${obj[key].name}</td>
                ${tempHTML}
          </tr>
           `
    }
    $("tbody").html(trHTML);
    const inputs = document.querySelectorAll("tbody td input");
    inputs.forEach((v,i)=>{
      inputs[i].addEventListener("blur", function (e) {
        foucsvalue=e.target.value;
        const data = {
          "stu_id":e.target.dataset.id,
          "batch":e.target.dataset.batch,
          "score":e.target.value,
        }
        tmep=e.target.value;
        axios.post('/score/entry', data)
          .then(res => {
            if(res.data.code===0){
              e.target.style.display="none";
              if(spanValue!==foucsvalue){
                toastr.success(res.data.message);
                getScoreList();
              }
            }
            console.dir(res);
          }).catch(err => {
           if(err.response.status===400){
             e.target.style.display="none";
           }
          })
      })
    })
  })
}
//修改
tbody.addEventListener("dblclick", function (e) {
  const input=e.target.querySelector("input");
  if (e.target.nodeName === "TD") {
    spanValue= e.target.querySelector("span").innerText;
    input.style.display = "block";
    input.focus();
  } else if (e.target.classList.contains("LX")) {
    e.target.parentNode.querySelector("input").style.display = "block";
    e.target.parentNode.querySelector("input").focus();
  }
})

getScoreList();